﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using KeepItFreeMvc.Models;

namespace KeepItFreeMvc.Controllers
{

        public class EventController : ApiController
        {
            private KeepItFreeMvc.Models.KeepItFreeEntities1 repository = new KeepItFreeMvc.Models.KeepItFreeEntities1();

            public IEnumerable<Event> GetAllEvents()
            {
                return repository.Event.ToList();
            }

            public Event GetEventById(int id)
            {
                var evento = repository.Event.FirstOrDefault((p) => p.Id == id);
                if (evento == null)
                {
                    //var resp = new HttpResponseMessage(HttpStatusCode.NotFound);
                    //throw new HttpResponseException(resp);
                }
                return evento;
            }

            public IEnumerable<Event> GetEventByName(string name)
            {
                return repository.Event.ToList().Where(
                    (p) => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
            }

            public IEnumerable<User> GetUsersByEventId(int id)
            {
                var users = repository.UserEvent.Where(p => p.EventId.Equals(id)).ToList();
                return repository.User.Where(u => u.Id.Equals(users)).ToList();                    
            }

            public IEnumerable<Tag> GetTagsByEventId(int id)
            {
                var tags = repository.TagEvent.Where(p => p.EventId.Equals(id)).ToList();
                return repository.Tag.Where(t => t.Id.Equals(tags)).ToList();
            }
        }

}
